package plugin;

import iddss.Parameters;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;


/**
 *
 * @author zem
 */
public class PluginLoader {
    public static boolean Error;
    static PluginInterface instance;
    static String plugin_name;

    /**
     * Load plugin
     * @param name
     * @return true if loaded successfully
     */
    public boolean Load(String name) {
        Error=false;
        plugin_name=name;

        File jar = new File(Parameters.PluginDirectory + File.separatorChar + name+File.separatorChar+name+".jar");


        // Checking plugin
       
            try {
                URL jarURL = jar.toURI().toURL();
                URLClassLoader classLoader = new URLClassLoader(new URL[]{jarURL});
                //getting main class
                String mainclass = new Plugin().GetMainClass(jarURL);

                boolean ok=true;
                if (mainclass != null) {  //if main class has been found
                    //load
                    Class pluginClass = classLoader.loadClass(mainclass);
                    instance = (PluginInterface) pluginClass.newInstance();

                    String[] message={name};
                   
                } else {
                    Error=true;
                     String[] mes = {name};
                   
                    ok=false;
                }

           if(ok) {return true;} else return false;
            } catch (Exception e) {
                System.out.println(e);
                  return false;
            }

        
    }

    /**
     * Start plugin
     * @return true if start successfully
     */
    public boolean Start() {
    try {
            Error=false;
             String[] mes = {plugin_name};
            
            instance.Start();
            return true;
        } catch (Exception er) {

            Error=true;
              String[] mes = {plugin_name};
            
             return false;
        }
    }

    /**
     * Stop plugin
     * @return true if success
     */
    public boolean Stop() {
        try {
            Error=false;
            instance.Stop();
            return true;
        } catch (Exception er) {
            Error=true;
              String[] mes = {plugin_name};
           
            return false;
        }
    }

    /**
     * Check if plugin has been crashes
     * @return true if crashed
     */
    public boolean Crashed() {
        try {
            Error=false;
            return instance.Crashed();
        } catch (Exception error) {
            Error=true;
              String[] mes = {plugin_name};
           
            return false;
        }

    }
    /**
     * Get crash error
     * @return crash error
     */
    public String CrashError() {
        try {
            Error=false;
            return instance.CrashError();
        } catch (Exception error) {
            Error=true;
             String[] mes = {plugin_name};
            
            return null;
        }

    }
    
    
    /**
     * Check if plugin need connection
     * @return true if needed
     */
    public boolean NeedConnection() {
    try {
            Error=false;
            return instance.NeadConnection();
        } catch (Exception error) {
            Error=true;
             String[] mes = {plugin_name};
          
            return false;
        }
    }

   

    /**
     * Input plugin connection number
     * @param number connection number to input
     * @return true if successfully input
     */
    public boolean InputConnectionNumber(int number) {
        try{
        instance.InputConnectionNumber(number);
        return true;
        }catch(Exception er){return false;}
    }






     /**
     * Check if plugin need user name who made the connection
     * @return true if needed
     */
    public boolean NeedUserName() {
    try {
            Error=false;
            return instance.NeadUserName();
        } catch (Exception error) {
            Error=true;
             String[] mes = {plugin_name};

            return false;
        }
    }



    /**
     * Input user name who made the connection
     * @param user_name name of user who made connection
     * @return true if successfully input
     */
    public boolean InputUserName(String user_name) {
        try{
        instance.InputUserName(user_name);
        return true;
        }catch(Exception er){return false;}
    }
}


